package com.mueeee.webspringboot3.utils;

import io.micrometer.common.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.env.Environment;

import java.net.URI;

/**
 * @author ch
 * @date 2023/11/24
 */
public class OpenWebpageUtils {
    private static final Logger log = LoggerFactory.getLogger(OpenWebpageUtils.class);

    public static void open(String url) {
        try {
            final Environment environment = EnvironmentUtils.environment;
            if (environment.getProperty("server.ssl.enabled", Boolean.class, StringUtils.isNotEmpty(environment.getProperty("server.ssl.key-store")))) {
                if (!url.contains("https")) {
                    url = url.replace("http", "https");
                }
            }
            // 创建一个URI对象，指定要打开的网页地址
            URI uri = new URI(url);
            CommandUtil.browse(uri);
        } catch (Exception e) {
            log.error("error", e);
        }
    }
}
